package liumengbaio.lolzhangyoubao.Activity.Activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

import liumengbaio.lolzhangyoubao.Activity.Adapter.MoreTreasureAdapter;
import liumengbaio.lolzhangyoubao.Activity.InFo.MoreTreasureInfo;
import liumengbaio.lolzhangyoubao.Activity.InFo.MoreTreasureWheelInfo;
import liumengbaio.lolzhangyoubao.Activity.View.lamp.LampBean;
import liumengbaio.lolzhangyoubao.Activity.View.lamp.VerticalLampView;
import liumengbaio.lolzhangyoubao.Activity.utils.HttpUtils;
import liumengbaio.lolzhangyoubao.R;

/**
 * “我的夺宝”界面
 * Created by lanouhn on 16/9/7.
 */
public class MoreTreasureActivity extends AppCompatActivity {
    private String urlHead = "http://lol.bet.zhangyoubao.com/service/rest?api=raid.goodslist&game=lol&platformVersion=400901";
    private String urlBody = "apiVersion=v1&platform=android&os=android&osVersion=19&secretSignature=IZk1Dd2GAu%" +
            "2BtjjBMEtBwyqO1dHs%3D&game=lol&sign=&platformVersion=400901&time=1473210831&secretId=AKIDz8krbsJ5yKBZQ" +
            "pn74WFkmLPx3gnPhESA&nonce=566999&userToken=1285c08b612411d0bfc35f213eb5d92a73&secretVersion=v1.0&userId=" +
            "15863305&api=raid.goodslist&deviceId=JfoZvarCyLfJIqs9SRFolapk21%2FwEw63JVMaX7Y3v5w%3D&params%5Bsort%5D=0&" +
            "params%5Bcurrency_type%5D=a&";

    private String urlWheelHead = "http://lol.bet.zhangyoubao.com/service/rest?api=raid.scrollingmessage&game=lol&platformVersion=400901";
    private String urlWheelBody = "apiVersion=v1&platform=android&os=android&osVersion=19&secretSignature=Ua2Z1pYArJttXIPL1ZI" +
            "cr5GOQkw%3D&game=lol&sign=&platformVersion=400901&time=1473210831&secretId=AKIDz8krbsJ5yKBZQpn74WFkmLPx3gnPhESA" +
            "&nonce=705997&userToken=1285c08b612411d0bfc35f213eb5d92a73&secretVersion=v1.0&userId=15863305&api=raid.scrolling" +
            "message&deviceId=JfoZvarCyLfJIqs9SRFolapk21%2FwEw63JVMaX7Y3v5w%3D&";
    private GridView mGridView;
    private MoreTreasureInfo mMoreTreasureInfo;
    private MoreTreasureWheelInfo mMoreTreasureWheelInfo;
    private MoreTreasureAdapter mMoreTreasureAdapter;
    private VerticalLampView mVerticalMarqueeView;
    private LinearLayout explain, my;
    private ImageView back;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.more_treasure);
        initView();
        getWheelData();
        initEvent();
        getDataFromWeb();
    }

    private void initView() {
        mGridView = (GridView) findViewById(R.id.more_treasure_gridView);
        mVerticalMarqueeView = (VerticalLampView) findViewById(R.id.more_treasure_wheel);
        explain = (LinearLayout) findViewById(R.id.more_treasure_explain);
        my = (LinearLayout) findViewById(R.id.more_treasure_my);
        back = (ImageView) findViewById(R.id.more_treasure_back);
    }

    private void initData() {
        List<LampBean> stringList = new ArrayList<>();
        for (int i = 0; i < mMoreTreasureWheelInfo.getData().size(); i++) {
            MoreTreasureWheelInfo.DataBean dataBean = mMoreTreasureWheelInfo.getData().get(i);
            LampBean bean = new LampBean();
            bean.title = "活动";
            bean.info = "恭喜 < " + dataBean.getUser_name() + " > 获得" + dataBean.getReward();
            stringList.add(bean);
        }
        mVerticalMarqueeView.setData((ArrayList<LampBean>) stringList);
        mVerticalMarqueeView.setTextSize(15);
        mVerticalMarqueeView.setTimer(2000);
        mVerticalMarqueeView.start();
    }

    private void initEvent() {
        explain.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MoreTreasureActivity.this, MoreTreasureExplainActivity.class);
                startActivity(intent);
            }
        });
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        my.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MoreTreasureActivity.this, MoreTreasureMyActivity.class);
                startActivity(intent);
            }
        });
        mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(MoreTreasureActivity.this, MoreTreasureDetailsActivity.class);
                intent.putExtra("unit", mMoreTreasureInfo.getData().get(position).getUnit_name());
                intent.putExtra("good_id", mMoreTreasureInfo.getData().get(position).getGoods_id() + "");
                intent.putExtra("goodAmount", mMoreTreasureInfo.getData().get(position).getPrice() + "");
                startActivity(intent);
            }
        });
    }

    /**
     * 获取跑马灯里的内容
     */
    private void getWheelData() {
        try {
            HttpUtils.doPostAsyn(urlWheelHead, urlWheelBody, new HttpUtils.CallBack() {
                @Override
                public void onRequestComplete(String result) {
                    Gson gson = new Gson();
                    mMoreTreasureWheelInfo = gson.fromJson(result, MoreTreasureWheelInfo.class);
                }
            }, handler1);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取GridView里的内容
     */
    private void getDataFromWeb() {
        try {
            HttpUtils.doPostAsyn(urlHead, urlBody, new HttpUtils.CallBack() {
                @Override
                public void onRequestComplete(String result) {
                    Gson gson = new Gson();
                    mMoreTreasureInfo = gson.fromJson(result, MoreTreasureInfo.class);
                }
            }, handler);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    mMoreTreasureAdapter = new MoreTreasureAdapter(MoreTreasureActivity.this, mMoreTreasureInfo);
                    mGridView.setAdapter(mMoreTreasureAdapter);
                    break;
                case 1:
                    Toast.makeText(MoreTreasureActivity.this, "获取数据失败！", Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    };
    private Handler handler1 = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    initData();
                    break;
                case 1:
                    Toast.makeText(MoreTreasureActivity.this, "获取数据失败！", Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    };
}
